package com.qf.listen;

import com.alibaba.fastjson.JSONObject;
import com.qf.clients.OrderClients;
import com.qf.pojo.entity.QfOrder;
import com.qf.pojo.repVo.BaseResp;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class RabbitMqListen {
    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    RedisTemplate redisTemplate;
    @Value("${spring.mail.username}")
    private String from;
    //定义日志
    public static Logger logger= LoggerFactory.getLogger(RabbitMqListen.class);
    @RabbitListener(queues = "qflive-email")
    public void sendMail(Map map, Channel channel, Message message){
        //1.info,error,waring,debug,trace
        try {
            logger.debug("进入了邮件发送的消费者，参数为：{}"+map);
            String email = map.get("email").toString();
            String code = map.get("code").toString();
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setSubject("千锋在线教育验证码");
            simpleMailMessage.setTo(email);
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setText("验证码为："+code);
            //发送邮件
            javaMailSender.send(simpleMailMessage);
            //将验证码存储在redis中！
            redisTemplate.opsForValue().set(email,code);
            //同时设置5分钟的过期时间
            redisTemplate.expire(email,5, TimeUnit.MINUTES);
            logger.debug("邮件发送成功！");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("邮箱发送失败！");
        }
    }
    @Autowired
    OrderClients orderClients;
    @RabbitListener(queues = "pay-queue")
    public void payQueue(String msg, Channel channel, Message message){
        try {
            logger.debug("进入到了取消支付的监听中。取消的订单号为：{}"+msg);
            //判断该订单的支付状态，如果状态还是为3的话，说明用户未在30秒内进行付款，则置为取消，如果状态已经为2，说明用户已经支付成功
            BaseResp byOrderid = orderClients.findByOrderid(msg);
            if (byOrderid.getCode()!=0){
                Object data = byOrderid.getData();
                String s = JSONObject.toJSONString(data);
                QfOrder qfOrder = JSONObject.parseObject(s, QfOrder.class);
                logger.debug("订单编号为：{}"+msg+"未在规定时间内付款，需要将其改为取消支付状态！");
                if (qfOrder.getOrderStatus()==3){
                    //远程调用order服务将该订单置为取消状态！
                    BaseResp baseResp = orderClients.updateOrderStatus(msg, 4);
                    if (baseResp.getCode()==1){
                        logger.debug("订单编号为：{}"+msg+"取消支付成功!");
                    }
                }else {
                    logger.debug("订单编号为：{}"+msg+"支付成功!");
                }

            }
            //将该笔订单消息进行确认，已经被处理完成
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
